package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.annotation.Task;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.ExcelUtils;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.data.InsDataBean;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.pages.backend.BackMenuList;
import com.focus.xyz.union.pages.backend.tables.MemTable;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenmeng on 2016/5/24.
 */
public class AllProdToubaoTest extends TC {
    private static transient Logger logger = LoggerFactory.getLogger(AllProdToubaoTest.class);

    private WebDriver driver;
    private KeyWordAction action;
    private ExcelUtils eu;

    @BeforeClass
    public void beforeClass() throws Exception{
        eu = new ExcelUtils(ConfigPropInfo.TEST_DATA_DIR + ConfigPropInfo.TEST_UNION_DATA_FILE);
        action.DelUnionFrontCookie();
    }

    @Parameters({"nodeUrl","browser","memName","memPass"})
    @BeforeMethod(alwaysRun = true)
    public void setUp(String nodeUrl,String browser,String memName,String memPass) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        super.init(driver);
        action = new KeyWordAction(driver);
        action.LoginFront(memName,memPass);
    }


    @Parameters({"browser"})
    @AfterMethod(alwaysRun = true)
    public void tearDown(String browser) {
        try{
            super.quit();
            driver.quit();
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }

    @DataProvider(name = "insData")
    public Object[][] getInsData() throws Exception{
        String[][] data = null;
        try {
            eu.setSelectedSheetName("insData");
            eu.setOnlyReadOneSheet(true);
            eu.setStartReadPos(1);
            data = eu.readExcelArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }


    @Task(taskName = "DEL_PRD_SCOPE_PLAT.sql",paras = {"param0=cimon009"})
    @Test(description = "将产品列表分配给某个代理人")
    @Parameters({"memName","userName","password"})
    public void ASSIGN_PROD(String memName,String userName,String password) throws Exception{
        action.LoginBack(userName,password);
        BackMenuList menuList = new BackMenuList(driver,action);
        action.pause(500l);
        menuList.openExpandMenu("渠道会员管理","查询渠道会员");
        MemTable memTable = new MemTable(driver,action);
        memTable.search(memName);
        action.pause(1000l);
        memTable.openLink(memName,"查看产品");
        action.pause(2000l);
        action.SetProdScopeAndPlat(getProdList(), Constants.K_AGENT);
    }


    @Test(dataProvider = "insData",description = "所有类型的产品投保流程验证",dependsOnMethods = {"ASSIGN_PROD"})
    public void TOUBAO(String id,String prodType,String prodName,String insPlanId,String insuredId,String applicantId,String desc,String expectedPrice) throws Exception{
        action.OpenToubaoPage(prodName);
        InsDataBean insData = InsDataService.getInsDataById(id);
        if(Constants.PROD_TYPE_T.equals(prodType.trim())){
            //团体险，上传模板清单
            action.TuantiToubao(insData);
        }else{
            action.CommonToubao(insData);
        }
        action.pause(2000l);
        action.assertStringFalse("投保信息不正确");//验证订单是否创建成功
        action.AcctPay(expectedPrice,"ins123");
    }

    private List<String> getProdList() throws Exception{
        List<String> list = new ArrayList<String>();
        try {
            eu.setSelectedSheetName(Constants.INS_DATA);
            eu.setOnlyReadOneSheet(true);
            eu.setStartReadPos(1);
            String[][] data = eu.readExcelArray();
            for(String[] ss: data){
                list.add(ss[2]);
            }
        } catch (Exception e) {
            logger.info("读取产品数据异常" + e);
        }
        return list;
    }
}
